/* * Copyright 2013 Andriy Vityuk * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.vityuk.ginger.maven; import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; public class ClasspathUtils { private ClasspathUtils() { } public static List<URL> getResources(MavenProject project) throws MojoExecutionException { return getResources(project, false); } public static List<URL> getResources(MavenProject project, boolean testClasspath) throws MojoExecutionException { try { Set<String> classpathElements = new HashSet<String>(); classpathElements.addAll(project.getCompileClasspathElements()); classpathElements.addAll(project.getRuntimeClasspathElements()); if (testClasspath) { classpathElements.addAll(project.getTestClasspathElements()); } List<URL> projectClasspathList = new ArrayList<URL>(); for (String element : classpathElements) { try { projectClasspathList.add(new File(element).toURI().toURL()); } catch (MalformedURLException e) { throw new MojoExecutionException(element + " is an invalid classpath element", e); } } URL[] urls = projectClasspathList.toArray(new URL[projectClasspathList.size()]); return Arrays.asList(urls); } catch (DependencyResolutionRequiredException e) { throw new MojoExecutionException("Dependency resolution failed", e); } } }